//
// LevelLoader.cpp for levelloader in /home/lefebv_w//workspace/cpp/bomberman/googleCode/level
// 
// Made by damien lefebvre
// Login   <lefebv_w@epitech.net>
// 
// Started on  Mon May 27 16:21:32 2013 damien lefebvre
// Last update Tue May 28 22:55:02 2013 damien lefebvre
//

#include	"LevelLoader.hh"

LevelLoader::LevelLoader(std::string const & path)
  : ALevel()
{
  std::ifstream		file(path.c_str(), std::ios::in);
  std::string		line;
  std::string		info;
  unsigned int		nbPlayer;
  size_t		lastPos;
  char	const		*c_str;
  Vector3f		pos;
  AEntity *		player;
  Vector3f		playerPos;

  std::cout << "----- LOADING LEVEL '" << path << "' -----" << std::endl; 

  /* 3 premiere ligne = texture */
  getline(file, line); // WALL
  info = line.substr(line.find(SEP)+1, line.size()-line.find(SEP)-1);
  std::cout << "loading wall texture : " << info << std::endl;  
  m_texs[Box::WALL] = info;
  getline(file, line); // FLOOR
  info = line.substr(line.find(SEP)+1, line.size()-line.find(SEP)-1);
  std::cout << "loading floor texture : " << info << std::endl;  
  m_texs[Box::FLOOR] = info;
  getline(file, line); // TILL
  info = line.substr(line.find(SEP)+1, line.size()-line.find(SEP)-1);
  std::cout << "loading till texture : " << info << std::endl;  
  m_texs[Box::TILL] = info;
   
  /* screenshot miniature */
  getline(file, line);
  info = line.substr(line.find(SEP)+1, line.size()-line.find(SEP)-1);
  std::cout << "loading screenshot : " << info << std::endl;  
  m_screenShot = info;

  /* sizes level, size box && nb player */
  getline(file, line);
  lastPos = line.find(SEP)+1;
  m_size_x = atoi(line.substr(lastPos, line.find(SEP, lastPos + 1) - lastPos).c_str());
  lastPos = line.find(SEP, lastPos + 1)+1;
  m_size_y = atoi(line.substr(lastPos, line.find(SEP, lastPos + 1) - lastPos).c_str());
  lastPos = line.find(SEP, lastPos + 1)+1;
  m_sizeBox = atoi(line.substr(lastPos, line.find(SEP, lastPos + 1) - lastPos).c_str());
  lastPos = line.find(SEP, lastPos + 1)+1;
  nbPlayer = atoi(line.substr(lastPos, line.find(SEP, lastPos + 1) - lastPos).c_str());
  std::cout << "getting level info : sizeLevel (" << m_size_x << ", " << m_size_y << ") - sizeBox (" << m_sizeBox << ") - nbPlayer (" << nbPlayer << ")" << std::endl; 

  /* map descriptor */
  playerPos = m_startMap;
  pos = m_startMap;
  for (unsigned int i = 0; i < m_size_x; i++)
    {
      getline(file, line);
      c_str = line.c_str();
      m_map.push_back(std::vector<Box>());
      for (unsigned int j = 0; j < m_size_y; j++)
	 {
	   Box::t_TypeOfCase	type = Box::FLOOR;
	   for (unsigned int k = 0; k < 3; k++)
	     {
	       if (charList[k].c == c_str[j])
		 type = charList[k].t;
	       if (c_str[j] == 'P')
		 {
		   playerPos.x = pos.x + m_sizeBox / 2;
		   playerPos.y = pos.y + m_sizeBox / 2;
		   playerPos.z = pos.z;
		 }
	     }
	   m_map[i].push_back(Box(pos, m_sizeBox, type, m_texs[type]));
	   pos.y = pos.y + m_sizeBox;
	 }
      pos.y = m_startMap.y;
      pos.x = pos.x + m_sizeBox;
     }

  std::cout << "loading players" << std::endl;
  player = new Player(playerPos, 


  std::cout << "level '" << path << "' loaded" << std::endl;
}
